//
//  PywCheckPayPwdApi.m
//  PywSdk
//
//  Created by smallwave on 16/9/10.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywCheckPayPwdApi.h"
#import "AIFCheckPayPwdManager.h"
#import "PropertyListReformer.h"
#import "PywMBProgressHUD+Pyw.h"

@interface PywCheckPayPwdApi ()<ETAPIManagerCallBackDelegate>

@end

@implementation PywCheckPayPwdApi

- (void)requestCheckPayPwdWithPayPwd:(NSString *)payPwd
                             success:(Successd)success
                                fail:(Failure)fail{
    self.successd = success;
    self.failure = fail;
    
    AIFCheckPayPwdManager *manager = [AIFCheckPayPwdManager manager];
    manager.pay_pwd = payPwd;
    manager.delegate = self;
    [manager loadData];
}

#pragma mark - ETAPIManagerCallBackDelegate
/**
 *  请求成功
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallAPIDidSuccess:(ETApiBaseManager *)manager
{
    PropertyListReformer *reformer = [[PropertyListReformer alloc] init];
    NSDictionary *dict = [manager fetchDataWithReformer:reformer];
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    [self checkPayPwdWithDict:dict];
}
/**
 *  请求失败
 *
 *  @param manager <#manager description#>
 */
- (void)managerCallApiDidFailed:(ETApiBaseManager *)manager
{
    [PywMBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    
    if (self.failure) {
        self.failure([self errorWithCode:0]);
    }
}
/**
 *  校验支付密码
 */
- (void)checkPayPwdWithDict:(NSDictionary *)dict{
    int resultCode = [[dict objectForKey:@"ack"] intValue];
    switch (resultCode) {
        case 200:
        {
            if (self.successd) {
                self.successd(nil,nil);
            }
        }
            break;
            
        default:
        {
            NSString *msg = [NSString stringWithFormat:@"%@",dict[@"msg"]];
            [PywMBProgressHUD showError:msg];
            if (self.failure) {
                self.failure([self errorWithCode:resultCode]);
            }
        }
            break;
    }
    
}

@end
